import { requestApi } from '../../../api'
import { formatImage } from '../../../utils/formatImage'
import { formatPrice } from '../../../utils/util'

const app = getApp()

// feed_type == 11 的详情页无关注和收藏按钮，

Page({
  data: {
    feedId: '',
    detail: {},

    // 请求状态，限制频繁请求
    attentionRequestFlag: false,
    favoriteRequestFlag: false,
    praiseRequestFlag: false,

    // tips弹框：主意用于“心愿单”收藏功能
    tips: {
      visible: false,
      title: '',
      msg: '',
    },
  },

  onLoad(options) {
    // app端二维码分享
    if (options.scene && options.scene.length) {
      const scene = decodeURIComponent(options.scene)
      const paramArr = scene.split(',')

      if (paramArr[1] == 'sl') {
        app.shortUrlToLong(paramArr[0], res => {
          this.data.feedId = res.feed_id

          if (res.channel) {
            app.data.channel = res.channel
          }
        })
      } else {
        this.data.feedId = paramArr[0]

        if (paramArr[1]) {
          app.data.channel = paramArr[1]
        }
      }
    } else {
      this.data.feedId = options.feed_id
    }
  },

  onShow() {
    wx.showLoading({
      title: '加载中...',
    })

    // 获取详情
    this.getDetailData()
  },

  // 获取详情
  getDetailData() {
    app.request({
      url: requestApi.getCommunityContentDetail,
      login: false,
      data: {
        feed_id: this.data.feedId
      },
    }, data => {
      const detail = data.original.data

      detail.blogger_info.avatar_full = formatImage.cdnImage(detail.blogger_info.avatar, formatImage.THUMBNAIL_M)
      detail.detail_list.cover_image.path_full = formatImage.cdnImage(detail.detail_list.cover_image.path, formatImage.THUMBNAIL_M)

      // feed类型，1长图文，2图片集，3短视频，4商品集，5商家买家秀, 11用户买家秀
      if (detail.detail_list.feed_type === 3) {
        detail.detail_list.video.path_full = formatImage.cdnVideo(detail.detail_list.video.path)
      }

      if (detail.detail_list.image_list) {
        detail.detail_list.image_list.forEach(item => {
          item.path_full = formatImage.cdnImage(item.path, formatImage.THUMBNAIL_M)
        })
      }

      detail.spu_list.forEach(item => {
        item.goods_price_format = formatPrice(item.goods_price  / 100)
        item.cover_image.path_full = formatImage.cdnImage(item.cover_image.path, formatImage.THUMBNAIL_M)
      })

      this.setData({
        detail,
      })

    }, error => {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: error,
      })
    }, () => {
      wx.hideLoading()
    })
  },

  // 点击达人头像和名称，弹出提示框
  showModal() {
    wx.showModal({
      title: '',
      showCancel: false,
      confirmText: '知道了',
      content: '更多达人SHOW，请下载OFashion迷橙APP',
    })
  },

  // 关注
  attention(e) {
    if (!app.toLogin()) return

    if (this.data.attentionRequestFlag) return

    this.data.attentionRequestFlag = true

    // 0 未关注，1 已关注
    const attention = e.currentTarget.dataset.attention
    const id = e.currentTarget.dataset.id

    app.request({
      url: requestApi.attentionBlogger,
      data: {
        blogger_id: id,

        // 1 关注，2 取消
        type: attention ? 2 : 1, 
      },
    }, data => {
      this.setData({
        'detail.blogger_info.is_attention': attention ? 0 : 1,
      })

      this.showTips(attention === 0 ? '关注成功，可在我的-关注中查看' : '关注已取消')
    }, error => {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: error,
      })
    }, () => {
      wx.hideLoading()
      this.data.attentionRequestFlag = false
    })
  },

  // 收藏
  favoriteShow(e) {
    if (!app.toLogin()) return

    if (this.data.favoriteRequestFlag) return 

    this.data.favoriteRequestFlag = true

    let isCollected = this.data.detail.blogger_info.is_collection

    app.request({
      url: isCollected ? requestApi.cancelCollectCommunityContent : requestApi.setCollectCommunityContent,
      data: {
        feed_id: this.data.detail.detail_list.feed_id
      },
    }, data => {
      this.setData({
        'detail.blogger_info.is_collection': isCollected ? 0 : 1,
      })

      const { title, msg } = {
        title: isCollected ? '已取消收藏' : '收藏成功',
        msg: isCollected ? '' : '可在“我的-收藏中查看”',
      }

      this.showTips(title, msg)
    }, error => {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: error,
      })
    }, () => {
      wx.hideLoading()
      this.data.favoriteRequestFlag = false
    })
  },

  // 点赞：feed_type不为11的走不同的点赞接口
  praiseShow(e) {
    if (!app.toLogin()) return

    if (this.data.praiseRequestFlag) return

    this.data.praiseRequestFlag = true

    let data = {}
    let url = ''
    let isLike = this.data.detail.blogger_info.is_liked

    if (this.data.detail.detail_list.feed_type === 11) {
      data = {
        comment_id: this.data.detail.detail_list.feed_id,
      }
      
      url = isLike ? requestApi.cancelLikeBuyerShow : requestApi.setLikeBuyerShow
    } else {
      data = {
        feed_id: this.data.detail.detail_list.feed_id,
      }

      url = isLike ? requestApi.cancelLikeCommunityContent : requestApi.likeCommunityContent
    }

    app.request({
      url,
      data,
    }, data => {
      this.setData({
        'detail.blogger_info.is_liked': isLike ? 0 : 1
      })
    }, error => {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: error,
      })
    }, () => {
      wx.hideLoading()
      this.data.praiseRequestFlag = false
    })
  },

  // 跳转到“主力spu详情页”
  toMainSpuDetail(e) {
    const spuNo = e.currentTarget.dataset.spuNo

    wx.navigateTo({
      url: `../../mainSpu/mainSpuDetail/mainSpuDetail?spu_no=${spuNo}`
    })
  },

  // 提示浮层
  showTips(title, msg = '', time = 3) {
    this.setData({
      tips: {
        title,
        msg,
        visible: true,
      }
    })

    setTimeout(() => {
      this.setData({
        'tips.visible': false,
      })
    }, 3000)
  },
})
